package com.ellricky.baselib.ui.clue

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.ui.ClueSelectActivity
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.jump
import com.google.android.material.tabs.TabLayout
import kotlinx.android.synthetic.main.base_activity_clue_lib.*

/**
 * 线索库
 */
class ClueLibActivity : BaseToolBarActivity<Void, BasePresenter<Void>>() {

    private lateinit var frgList: MutableList<Fragment>

    // 排序
    private val sortAsc = "asc"
    private val sortDesc = "desc"
    private var sort = sortDesc

    // 筛选项
    private var filter = ""
    private var byTime = "clueDate"
    private var byVisit = "backNum"
    private var byAge = "age"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LocalCacheUtil.setIsReloadData(true)
    }

    override fun onPause() {
        super.onPause()
        LocalCacheUtil.setIsReloadData(false)
    }

    override fun getContentLayout(): Int {
        return R.layout.base_activity_clue_lib
    }

    override fun initUIView() {
        super.initUIView()
        mTvTitle.text = getString(R.string.base_clue_lib)
        initTab()
        initSearchView()
    }


    private fun initTab() {
        frgList = ArrayList()
        frgList.add(CluePoolFragment.newInstance(""))
        frgList.add(CluePoolFragment.newInstance("31"))
        frgList.add(ClueCtFragment.newInstance())
        val tList = listOf("线索池", "回收站", "课程顾问")
        tabLayout.setUp(supportFragmentManager, frgList, tList, vp)
        vp.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                resetSearchView(position)
            }
        })
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                resetSearchView(tab?.position ?: 0)
            }
        })
    }


    private fun initSearchView() {
        ib_search.setOnClickListener(this)
        ibSort.setOnClickListener(this)
        tvTime.setOnClickListener(this)
        tvVisit.setOnClickListener(this)
        tvAge.setOnClickListener(this)
        tvClueSelect.setOnClickListener(this)
    }

    private fun resetSearchView(position: Int) {

        etSearch.setText("")

        sort = sortDesc
        ibSort.setImageResource(R.drawable.base_icon_clue_desc)

        filterView.visibility = View.GONE
        filter = ""


        when (vp.currentItem) {
            0 -> {
                cl_club_num.visibility = View.VISIBLE
                val fragment = frgList[vp.currentItem] as CluePoolFragment
                val num = fragment.getCurNumber()
                fragment.resetKeyword()
                tvClueNumber.text = "当前线索总条数：${num}条"
                searchView.visibility = View.VISIBLE
            }
            1 -> {
                cl_club_num.visibility = View.VISIBLE
                val fragment = frgList[vp.currentItem] as CluePoolFragment
                val num = fragment.getCurNumber()
                fragment.resetKeyword()
                tvClueNumber.text = "当前线索总条数：${num}条"
                searchView.visibility = View.VISIBLE
            }
            2 -> {
                cl_club_num.visibility = View.GONE
                searchView.visibility = View.GONE
            }
        }
    }

    override fun onClickView(v: View?) {
        super.onClickView(v)
        when (v) {
            ib_search -> {
                refreshData()
            }

            ibSort -> {
                when (sort) {
                    sortAsc -> {
                        sort = sortDesc
                        ibSort.setImageResource(R.drawable.base_icon_clue_desc)
                    }
                    sortDesc -> {
                        sort = sortAsc
                        ibSort.setImageResource(R.drawable.base_icon_clue_asc)
                    }
                }
                refreshData()
            }

            tvClueSelect -> {
                jump<ClueSelectActivity> {}
            }

            tvTime, tvVisit, tvAge -> {
                when (v) {
                    tvTime -> filter = byTime
                    tvVisit -> filter = byVisit
                    tvAge -> filter = byAge
                }

                filterView.visibility = View.GONE
                refreshData()
            }
        }
    }

    fun refreshClubNum(num: String) {
        tvClueNumber.text = "当前线索总条数：${num}条"
    }

    private fun refreshData() {
        val index = vp.currentItem
        val fragment = frgList[index]
        when (fragment) {
            is CluePoolFragment -> {
                fragment.search(etSearch.text.toString())
            }
        }
    }
}
